package com.fxhy;

import java.util.Properties;

import javax.servlet.MultipartConfigElement;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;

import com.fxhy.socket.SocketService;
import com.github.pagehelper.PageHelper;

@EnableAutoConfiguration
@SpringBootApplication
@MapperScan("com.fxhy.mapper")
@ImportResource(locations={"classpath:kaptcha-config.xml"}) 
@ComponentScan("com.fxhy.*")
@EnableScheduling
public class AgentApplication {
	
	@Bean
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}
	
	public static void main(String[] args) {
		SpringApplication springApplication = new SpringApplication(AgentApplication.class);
		ConfigurableApplicationContext configurableApplicationContext = springApplication.run(args);
		SocketService.setApplicationContext(configurableApplicationContext);//解决WebSocket不能注入的问题
	}
	
	@Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 设置一个临时目录
        if (isWindows()) {
        	factory.setLocation("C://picTemp/");
		}else {
			factory.setLocation("/opt/catchbaby-service/temp");
		}
        return factory.createMultipartConfig();
    }
	
	
	public boolean isWindows() {  
	    return System.getProperties().getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1;  
	}
	
	
	//配置mybatis的分页插件pageHelper
    @Bean
    public PageHelper pageHelper(){
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum","true");
        properties.setProperty("rowBoundsWithCount","true");
        properties.setProperty("reasonable","true");
        properties.setProperty("dialect","mysql");    //配置mysql数据库的方言
        pageHelper.setProperties(properties);
        return pageHelper;
    }
    
    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return (container -> {
                ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/errors/401.html");
                ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/errors/404.html");
                ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/errors/500.html");
                container.addErrorPages(error401Page, error404Page, error500Page);
        });
    }
}
